<?php

namespace Common\Model;
use Think\Model;

class GoodsModel extends Model {
    protected $trueTableName = 'tb_goods';

    public function _getWhere($map) {
        $where = array();
        empty($map['name']) ?: $where['name'] = array('like', '%' . $map['name'] . '%');
        empty($map['title']) ?: $where['title'] = array('like', '%' . $map['title'] . '%');
        empty($map['number']) ?: $where['number'] = $map['number'];
        empty($map['st']) ?  : $where["status"] = $map['st'];

        return $where;
    }

    public function findData($map, $page_num = 1, $page_size = 20) {
        $where = $this->_getWhere($map);
        $list = $this
            ->where($where)
            ->order('id desc')
            ->page($page_num . ',' . $page_size)
            ->select();

        return $list;
    }

    public function getDataNum($map) {
        $where = $this->_getWhere($map);
        $count = $this->where($where)->count();
        return $count;
    }

    public function getOneData($id)
    {
        $oneData = $this->where('id =' . $id)->find();
        return $oneData;
    }

   public function getStarsList()
   {
       $where = array();
       $where['status']=1;
       $stars=$this->where($where)->field('id,name')->select();
       return $stars;
   }

    public function getStarsListInIds($nameList=0)
    {
        $where = array();
        $where['status']=1;
        $where['name'] = array('in',$nameList);

        $stars=$this->where($where)->field('id,name,avatar_zoom')->select();
        //var_dump($stars);

        // 明星头像显示最多三个
        $stars_rel = array();
        if(count($stars)>3){
           $stars_rel = array_slice($stars,0,3);
        }else{
            $stars_rel = $stars;
        }

        //var_dump($stars_rel);die();

        return $stars_rel;

    }


    public function  getStarsIdList($nameList=0)
    {
        if($nameList){

            $nameList = str_replace('#', ',', trim($nameList, '#'));

            $where = array();
            $where['status']=1;
            $where['name'] = array('in',$nameList);

            $stars=$this->where($where)->field('id')->select();

            return $stars;
        }else{

        }
    }
}